package com.portal.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.portal.common.enums.ErrorEnum;
import com.portal.common.enums.StatusEnum;

/**
 * 返回体
 *
 * @author Amadeus
 * @date 2022/07/07
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonBaseResult<T> {

    private T result;
    private String errorCode;
    private String errorMessage;
    private StatusEnum resultStatus = StatusEnum.SUCCEED;

    public void setErrorCode(ErrorEnum errorEnum) {
        this.errorCode = errorEnum.getCode();
        this.errorMessage = errorEnum.getMessage();
        this.resultStatus = StatusEnum.FAILED;
    }


    public void setError(String msg) {
        this.errorMessage = msg;
        this.resultStatus = StatusEnum.FAILED;
    }

    public static <T> JsonBaseResult<T> success(T data) {
        JsonBaseResult<T> result = new JsonBaseResult<>();
        result.setResult(data);
        result.setResultStatus(StatusEnum.SUCCEED);
        return result;
    }


    public static JsonBaseResult<Object> success() {
        JsonBaseResult<Object> result = new JsonBaseResult<>();
        result.setResultStatus(StatusEnum.SUCCEED);
        return result;
    }

    public static JsonBaseResult<Object> fail() {
        JsonBaseResult<Object> result = new JsonBaseResult<>();
        result.setResultStatus(StatusEnum.FAILED);
        result.setErrorMessage("异常错误");
        return result;
    }


    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public StatusEnum getResultStatus() {
        return resultStatus;
    }

    public void setResultStatus(StatusEnum resultStatus) {
        this.resultStatus = resultStatus;
    }
}

